clear
capture log close

log using LogFile_PreparedPublicReplicationFileStudy2.txt, replace text

* This .do file loads the .dta export from qualtrics for Study 2
* The raw data (identified, with IPs/etc.) is NOT included in the replication file.
* The outputted file from this code is Study2data_publicdatafile_cleaned.dta

* Load Study 2 data; extract prepared by Kyle Peyton
use "..\PrivateDataNotInReplicationArchive\Study2_lucid_july_clean.dta"
gen study="Study2"

* Fix a couple problems with coding of covariates
replace x_race="White" if x_race=="Other" & x_ethnicity =="White"
replace x_hhi=25 if x_hhi==.
label define x_hhi 25 "Refused", add

* Recode treatments
gen treat_officerwhite = Z_cop_race==1
label var treat_officerwhite "Officer White (1=yes)"
label def lblofrace 0 "Black" 1 "White"
label values treat_officerwhite lblofrace

gen S2treat_demeanor=.
/* NB: Checked raw data from Qualtrics to confirm coding
Coding is officer behavior citizen behavior, so PH means Polite officer Hostile citizen
         1=PP |        535       24.91       24.91
         2=PH |        529       24.63       49.53
         3=HP |        548       25.51       75.05
         4=HH |        536       24.95      100.00
*/
replace S2treat_demeanor=1 if Z_interaction==1
replace S2treat_demeanor=2 if Z_interaction==2
replace S2treat_demeanor=3 if Z_interaction==4
replace S2treat_demeanor=4 if Z_interaction==3
label var  S2treat_demeanor "Demeanor"
label define S2lbldemeanor 1 "Both Polite" 2 "Hostile Civilian, Polite Officer" 3 "Both Hostile (Officer first)" 4 "Polite Civilian, Hostile Officer" 
label values S2treat_demeanor S2lbldemeanor
tab S2treat_demeanor Z_interaction

gen treat_weapon = Z_weapon==1
label var  treat_weapon "Civilian has weapon (1=yes)"
label define lblweapon 0 "Unarmed" 1 "Baseball bat"
label values treat_weapon lblweapon
tab treat_weapon Z_weapon

gen treat_arrest = Z_outcome==2
label var treat_arrest "Assigned punishment (1=Arrested)"
label define lblarrest 0 "Ticketed" 1 "Arrested"
label values treat_arrest lblarrest
tab treat_arrest Z_outcome

* Arrest demeanor interactions
gen alt_treat_arst_dmnr=(S2treat_demeanor-1)*treat_arrest
label define altdemeanorlab3 0 "Arrested x Both Polite" 1 "Arrested x Hostile Civilian, Polite Officer" 2 "Arrested x Polite Civilian, Hostile Officer" 3 "Arrested x Both Hostile (Officer first)"
label values  alt_treat_arst_dmnr  altdemeanorlab3
label var alt_treat_arst_dmnr "Arrest x Demeanor"

gen treat_arst_weapon = treat_arrest*treat_weapon
label var treat_arst_weapon "Arrest x Weapon"
label define treat_arst_weaponlab 0 "Not Arrested or Unarmed" 1 "Arrested and Armed"
label values treat_arst_weapon  treat_arst_weaponlab
* Arrest race interactions

gen treat_arst_ow = treat_arrest*treat_officerwhite
label var treat_arst_ow "Arrest x Officer White"

* Recode outcomes

* Fairness to citizen
tab y_fair_treatciv_n
gen out_fairness_cit_vig1 = y_fair_treatciv_n
label define fairness 1 "Unfair" 2 "Somewhat Unfair" 3 "Neither" 4 "Somewhat fair" 5 "Fair"
label values out_fairness_cit_vig1 fairness
label variable out_fairness_cit_vig1 "Fairness to citizen (prior to learning punishment)"

* Threat measures
tab y_threat_tovic_n
tab y_threat_tocop_n
tab y_threat_tociv_n

gen out_threat_tovic_vig1 = y_threat_tovic_n
gen out_threat_tocop_vig1 = y_threat_tocop_n
gen out_threat_tociv_vig1 = y_threat_tociv_n
label define threatlab 1 "None" 2 "A little" 3 "Moderate" 4 "A lot" 5 "A great deal"
label values out_threat_tovic_vig1 out_threat_tocop_vig1 out_threat_tociv_vig1 threatlab
label variable out_threat_tovic_vig1 "Perceived threat to victim (by assailant)"
label variable out_threat_tociv_vig1 "Perceived threat to civilian (by officer)"
label variable out_threat_tocop_vig1 "Perceived threat to police (by assailant)"

* Expected punishment
tab y_predict_n
gen out_expectedpunish_vig1 = y_predict_n
label define exppunish 1 "Release" 2 "Release + escort" 3 "Verbal Warn" 4 "Ticket" 5 "Arrest"
label values out_expectedpunish_vig1 exppunish
label variable out_expectedpunish_vig1 "Severity of expected citizen punishment (prior to learning punishment)"

* Fairness of punishment
tab y_fair_outcome_civ_n
gen out_fairness_pun_civ_vig2 = y_fair_outcome_civ_n
gen out_fairness_pun_vic_vig2 = y_fair_outcome_vic_n
label values out_fairness_pun_civ_vig2 out_fairness_pun_vic_vig2 fairness
label variable out_fairness_pun_civ_vig2 "Fairness to citizen of assigned punishment"
label variable out_fairness_pun_vic_vig2 "Fairness to victim of assigned punishment"

* Preferred punishment
tab y_should_n
gen out_preferredpunish_vig2 = y_should_n
label values out_preferredpunish_vig2 exppunish
label variable out_preferredpunish_vig2 "Preferred Punishment"

* Global fairness
tab y_fair_global_n
gen out_fairness_global_vig2 = y_fair_global_n
label values out_fairness_global_vig2 fairness
label variable out_fairness_global_vig2 "Overall fairness to citizen of interaction"

rename out_fairness_cit_vig1 s2_out_fairnessb4p
rename out_threat_tovic_vig1 s2_out_threat_vic
rename out_threat_tocop_vig1 s2_out_threat_cop
rename out_expectedpunish_vig1 s2_out_expectedpun
rename out_fairness_pun_civ_vig2 s2_out_fairnesspun
rename out_preferredpunish_vig2 s2_out_preferpun
rename out_fairness_global_vig2 s2_out_fairnesglob

* restrict to cases with at least one outcome measurement
keep if s2_out_fairnessb4p!=. | s2_out_threat_vic!=. | s2_out_threat_cop!=. | s2_out_expectedpun!=. | s2_out_fairnesspun!=. | s2_out_preferpun!=. | s2_out_fairnesglob!=. | out_threat_tociv_vig1!=. | out_fairness_pun_vic_vig2!=.

* Anonymize data, remove variables from other studies and extra variables
drop admin_browser admin_useragent admin_latitude admin_longitude admin_rid admin_supplier y_fair_treatciv_n- y_fair_global_n Z_*
drop admin_* x_zip trust_pol_care_n trust_pol_admit_n trust_pol_race_n trust_pol_job_n trust_pol_spend_n trust_pol_info_n contact_past contact_f2f contact_arrest contact_quantity contact_binary_n contact_f2f_binary_n contact_freq_n safety_walk safety_walk_n fair_treat_binary_n div_guess_black_n- div_gap_male_n
drop y_imp_div_sex_n- y_trolley_asian_n

compress
save Study2data_publicdatafile_cleaned.dta, replace

log close
